package com.shenganyuan.smac.msg.adapter;

import android.view.ViewGroup;
import android.widget.TextView;

import com.oklib.widget.recyclerview.BaseQuickAdapter;
import com.oklib.widget.recyclerview.BaseViewHolder;
import com.shenganyuan.smac.R;
import com.shenganyuan.smac.msg.model.ChatMessage;

import java.util.List;

/**
 * 聊天界面
 *
 * @author Damon
 */
public class AdapterChat extends BaseQuickAdapter<ChatMessage, BaseViewHolder> {
    private List<ChatMessage> messages;

    public AdapterChat(List<ChatMessage> data) {
        super(data);
    }

    @Override
    protected void convert(BaseViewHolder helper, ChatMessage item) {
        //发送消息和接受消息的view id相同
        //发送消息
        TextView contentView = helper.getView(R.id.message_text_view);
        contentView.setText(item.getMessage());
        TextView txtSender = helper.getView(R.id.sender_text_view);
        txtSender.setText(item.getSender());
        TextView txtTime = helper.getView(R.id.timestamp_text_view);
        txtTime.setText(item.getFormattedTime());
    }

    @Override
    public int getDefItemViewType(int position) {
        if (getItem(position).getType() == ChatMessage.Type.RECEIVED) {
            return 100;
        } else {
            return 200;
        }
    }

    @Override
    public BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 100) {
            return new BaseViewHolder(getItemView(R.layout.chat_item_rcv, parent));
        } else {
            return new BaseViewHolder(getItemView(R.layout.chat_item_sent, parent));
        }

    }

}
